package cn.edu.neusoft.yuejian4.yuejian_4.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.PagerSlidingTabStrip;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.adapter.MyCollectionTrainAdapter;
import cn.edu.neusoft.yuejian4.yuejian_4.fragment.ActivityCollectionFragment;
import cn.edu.neusoft.yuejian4.yuejian_4.fragment.TrainCollectionFragment;
import cn.edu.neusoft.yuejian4.yuejian_4.model.MyModel;

/**
 * Created by Lix on 2018/10/24.
 */

public class MyCollectionActivity extends AppCompatActivity {
    private ImageView back;
    private SharedPreferences sp;
    private int uid;
    private MyCollectionTrainAdapter myCollectionTrainAdapter;

    private PagerSlidingTabStrip mPagerSlidingTabStrip;
    private ViewPager mViewPager;

    private ActivityCollectionFragment activityCollectionFragment=null;
    private TrainCollectionFragment trainCollectionFragment=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collection);
        sp = getSharedPreferences("userinfo", MODE_PRIVATE);
        uid = sp.getInt("uid", 4);
        initView();
        initEvent();
    }
    private void initView(){
        back = (ImageView) findViewById(R.id.my_coll_back);
        mViewPager = (ViewPager)findViewById(R.id.coll_viewpager);
        /*需要管理相互独立的并且隶属于Activity的Fragment使用FragmentManager（），
        而在Fragment中动态的添加Fragment要使用getChildFragmetManager（）来管理。*/
        mViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        mPagerSlidingTabStrip=(PagerSlidingTabStrip)findViewById(R.id.coll_pagerSliding);
        mPagerSlidingTabStrip.setViewPager(mViewPager);

    }

    //自定义ViewPagerAdapter子类
    private class MyPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = {"活动", "训练"};//显示在二级导航上的标题文字

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];//确定选择当页导航上文字
        }

        @Override
        public int getCount() {
            return titles.length;//二级导航个数
        }

        @Override
        public Fragment getItem(int position) {
            //根据位置返回具体某个导航上对应的Fragment
            switch (position) {
                case 0:
                    if (activityCollectionFragment == null) {
                        activityCollectionFragment = new ActivityCollectionFragment();
                    }
                    return activityCollectionFragment;
                case 1:
                    if (trainCollectionFragment == null) {
                        trainCollectionFragment = new TrainCollectionFragment();
                    }
                    return trainCollectionFragment;
                default:
                    return null;
            }


        }
    }
    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.my_coll_back:
                        finish();
                        break;

                }
            }
        });


    }


}
